const app = getApp()
var module=require("../../../utils/shanghai/qita.js");
var lang=app.globalData.lang;
Page({
  data: {
    companies:'',
  	language:'zh',
  	choose:'',
  	current:'',
  	currentChoose:'',
  	showChooseItems:false,
  	chooseItmes:null,
    inputShowed: false,
    regionSelected: false,
    regionShowed: false,
    region: [],
    regionTitle:'',
    sortSelected:false,
    sortShowed:false,
    inputVal: "",
    tabId: 0,
    businessScope: '',
    page:0,
    number:0,
    sort:[],
    isGoSetting:false,
  },
  onLoad: function (e) {
    var that = this;
    var lang = app.globalData.lang;
    that.setData({
      language: lang,
      current: '',
      currentChoose:module[lang].choose,
      tabId: 0,
      showChooseItems: false,
      chooseItmes: null,
      inputShowed: false,
      regionSelected: false,
      regionShowed: false,
      region: module[lang].region,
      inputVal: "",
      businessScope: module[lang].businessScope,
      sort: module[lang].sort
    });
    if (e.businessScope != undefined) {
      that.data.currentChoose.businessScope.id = e.businessScope;
      that.data.currentChoose.businessScope.title = e.title;
      that.setData({
        currentChoose: that.data.currentChoose,
        tabId: Number(e.index)
      });
    }
  },
  onShow: function(e) {
    var that=this;
    var lang=app.globalData.lang;
    if (lang != "zh") {
      wx.setNavigationBarTitle({
        title: 'Other'
      })
    }
    wx.getLocation({
    type: 'wgs84',
    success: function(res) {
    var latitude = res.latitude
    var longitude = res.longitude
    that.setData({
      language:lang,
      current:'',
      // currentChoose:module[lang].choose,
      // tabId: 0,
      showChooseItems:false,
      chooseItmes:null,
      inputShowed: false,
      regionSelected: false,
      regionShowed: false,
      region: module[lang].region,
      inputVal: "",
      location:latitude+','+longitude,
      // location:'31.275610,121.528210',
      businessScope: module[lang].businessScope,
      sort:module[lang].sort
    });
    that.changeCompanies(true);

  },
  // fail:function(){
  //   wx.navigateBack({
  //     delta:1
  //   })
  // }
    });
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          that.showRemind();
        }
      }
    });
},
   openSetting() {
    wx.openSetting({
      success: (res) => {
        if (!res.authSetting['scope.userLocation']) {
          showRemind();
        }
      }, fail: (res) => {
        if (!res.authSetting['scope.userLocation']) {
          showRemind();
        }
      }
    })
  },

showRemind () {
  var that = this;
    wx.showModal({
      title: '温馨提醒',
      content: '需要获取您的地理位置才能使用小程序',
      showCancel: true,
      confirmText: '获取位置',
      success: function (res) {
        if (res.confirm) {
          that.openSetting();
        }else{
          wx.navigateBack({
            delta: 1
          });
        }
      }, fail: (res) => {
        wx.navigateBack({
          delta:1
        });
      }
    })
  },
  call:function(e){
    const phone=e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  openMap:function(e){
    const position=e.currentTarget.dataset.position.split(',');
    const latitude=Number(position[0]);
    const longitude=Number(position[1]);
    wx.openLocation({
          latitude: latitude,
          longitude: longitude,
          name:e.currentTarget.dataset.name,
          address:e.currentTarget.dataset.address,
          scale: 14.5
    })
  },
  clearInput: function() {
    this.setData({
      inputVal: ""
    });
    this.changeCompanies(true);
  },
  inputTyping: function(e) {
    this.setData({
      inputVal: e.detail.value,
      showChooseItems: false
    });
    this.changeCompanies(true);
  },
  hideSelect:function(e){
    this.setData({
      showChooseItems: false
    });
  },
  resetFn:function(e){
    for(var i=0;i<Object.keys(this.data.region).length;i++){
      this.data.region[i].selected=false;
    }
    this.setData({
      region:this.data.region,
      regionTitle:'',
    });
  },
  submitFn:function(e){
    this.hideSelect();
    var j=0;
    this.data.currentChoose.region=[];
    for(var i=0;i<Object.keys(this.data.region).length;i++){
      if(this.data.region[i].selected){
        this.data.currentChoose.region[j++]=this.data.region[i].id;
      }
    }
    this.setData({
      currentChoose:this.data.currentChoose,
    });
    this.changeCompanies(true);
  },
  setTitle:function(){
    this.data.regionTitle='';
    for(var i=0;i<Object.keys(this.data.region).length;i++){
      if(this.data.region[i].selected){
        this.data.regionTitle+=this.data.regionTitle==''?this.data.region[i].title:','+this.data.region[i].title;
      }
    }
    this.setData({
      regionTitle:this.data.regionTitle,
    });
  },
  chooseRegionItem:function(e){
    const index=e.currentTarget.dataset.index;
    this.data.region[index].selected=!this.data.region[index].selected;
    this.setData({
        region:this.data.region, 
    });
    this.setTitle();
  },
  chooseSortItem:function(e){
    const index=e.currentTarget.dataset.index;
    this.data.currentChoose.sort.title=this.data.sort[Number(index)].title;
    this.data.currentChoose.sort.id=this.data.sort[Number(index)].id;
    this.setData({
      currentChoose:this.data.currentChoose,
    });
    this.hideFn();
    this.changeCompanies(true);
  },
  chooseSort:function(e){
    this.setData({
      sortSelected:true,
      regionSelected:false,
      showChooseItems:this.data.regionSelected?true:!this.data.showChooseItems
    });
  },
  chooseRegion: function(e) {
    this.setData({
      sortSelected:false,
    	regionSelected:true,
    	showChooseItems:this.data.sortSelected?true:!this.data.showChooseItems
    });
  },
  selectRegion:function(e){
    this.setData({
      regionSelected:false,
      showChooseItems:false,
    });
  },
  hideFn() {
    this.setData({
      showChooseItems: false,
      regionSelected:false,
      sortSelected:false,
    })
  },
  selectedTap: function(e) {
    const index = e.currentTarget.dataset.index;
    var lang = app.globalData.lang;
    console.log(lang);
    this.setData({
      tabId: index,
      currentChoose:{
        region:module[lang].choose.region,
        businessScope:{id:this.data.businessScope[Number(index)].id,title:this.data.businessScope[Number(index)].title},
        sort:module[lang].choose.sort,
      }, 
      region:module[lang].region,
      showChooseItems:false,
      inputVal:'',
      regionTitle:'',
      number:0,
    });
    this.changeCompanies(true);
  },

  appendCompanies:function(){
    this.changeCompanies(false);
  },
  changeCompanies:function(isRefresh){
    var that=this;
    wx.showLoading({
      title:'正在加载...'
    });
    wx.request({
      url:app.globalData.url+'other/showList',
      method:'POST',
      data:{
        businessScope:this.data.currentChoose.businessScope.id,
        region:this.data.currentChoose.region,
        language:this.data.language,
        title:this.data.inputVal,
        location:this.data.location,
        page:isRefresh?0:this.data.page+1,
        sort:this.data.currentChoose.sort.id,
        },
      success: function(res) {
      console.log(res);
      if(res.data.code==1){
        if(isRefresh){
          var companies=res.data.data.companies;
          var page=0;
        }else{
          var companies=that.data.companies;
          var page=that.data.page+1;
          for(var i=0;i<res.data.data.companies.length;i++){
          companies.push(res.data.data.companies[i]);
          }
        }
        console.log(page);
        that.setData({
          companies:companies,
          page:page,
        },function(){
          wx.hideLoading();
        });
      }else if(res.data.code==0){
          wx.showToast({
            title:res.data.msg,
            icon:"none"
          });
      }
      } 

    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    return {
      title: app.globalData.shareTitle,
      path: app.globalData.sharePath,
      imageUrl: '../../../images/share_img.png',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
})